﻿namespace DepartmentEditForm.DepartmentSpecificEditForm
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    public partial class DepartmentSpecificEditFormUserControl : UserControl
    {
        #region Fields

        private string currentDepartmentName = "";
        private string currentItemID = "";
        private string currentProjectID = "";

        #endregion Fields

        #region Methods

        public void GetAllListRowDataFromCollection( ref DataRow dataRow, SPWeb storeWeb, string projectID, string listName, List<DepartmentViewReferences> uniqueDataAccess )
        {
            string currentCode = "";

            SPList allOrderingFields = storeWeb.Lists[ "Department Ordering Lookup" ];
            string currentFieldName = "";
            string currentValue = "";

            SPQuery sourceUserDepartments = new SPQuery( );

            sourceUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + listName + "</Value></Eq></Where>";

            SPListItemCollection allFieldViews = allOrderingFields.GetItems( sourceUserDepartments );
            if ( allFieldViews.Count == 0 )
                return;

            DataRow[ ] dr;
            string originalCommentsValue = "";
            bool commentsField = false;

            DataRow drFixed = null;
            for ( int j = 0; j < allFieldViews.Count; j++ )
            {
                commentsField = false;
                currentFieldName = allFieldViews[ j ][ "Field Name" ].ToString( );
                if ( currentFieldName.Trim( ).Equals( "Comments" ) )
                    commentsField = true;

                foreach ( DepartmentViewReferences deptCollections in uniqueDataAccess )
                {
                    if ( deptCollections.DepartmentName.Trim( ).Equals( allFieldViews[ j ][ "Source Department" ].ToString( ).Trim( ) ) )
                    {
                        currentCode = deptCollections.DepartmentCode;
                        if ( deptCollections.DepartmentName.Equals( "Stores Detail" ) )
                        {
                            dr = deptCollections.DepartmentDataTable.Select( "Reference_x0020__x0023_='" + projectID + "'" );
                            drFixed = dr[ 0 ];
                            break;
                        }
                        else
                        {
                            dr = deptCollections.DepartmentDataTable.Select( "Store_x0020_Reference_x0020_ID='" + projectID + "'" );
                            drFixed = dr[ 0 ];
                            break;
                        }
                    }
                }

                if ( !currentFieldName.Trim( ).Equals( "Modified By" ) )
                {
                    if ( currentFieldName.Replace( " ", "_x0020_" ).Trim( ).Length > 32 )
                        currentValue = drFixed[ currentFieldName.Replace( " ", "_x0020_" ).Trim( ).Substring( 0, 32 ) ].ToString( );
                    else
                        currentValue = drFixed[ currentFieldName.Replace( " ", "_x0020_" ) ].ToString( );
                }
                else
                    currentValue = drFixed[ "Editor" ].ToString( );

                if ( dataRow.Table.Columns.Contains( currentFieldName.Trim( ) + " - " + currentCode ) )
                    currentFieldName = currentFieldName + " - " + currentCode;

                if ( !commentsField )
                    dataRow[ currentFieldName ] = Strip( DateStringCleanup( currentValue ) );
                else
                {
                    originalCommentsValue = currentValue;
                    currentValue = "<span class=\"commenttip1\"><a href=\"..\\Rollup%20Pages\\DepartmentComments.aspx?ProjectID=" + projectID + "&ListDeptName=" + allFieldViews[ j ][ "Source Department" ].ToString( ).Trim( ) + "\">";
                    if ( originalCommentsValue.Trim( ).Trim( ).Length > 63 )
                        currentValue += originalCommentsValue.Trim( ).Trim( ).Substring( 0, 63 ) + " ...";
                    else
                        if ( originalCommentsValue.Trim( ).Trim( ).Length == 0 )
                            currentValue += "View History";
                        else
                            currentValue += originalCommentsValue;

                    currentValue += "<span>";
                    if ( originalCommentsValue.Trim( ).Trim( ).Length > 150 )
                        currentValue += originalCommentsValue.Trim( ).Trim( ).Substring( 0, 150 ) + " ...";
                    else
                        if ( originalCommentsValue.Trim( ).Trim( ).Length == 0 )
                            currentValue += "Current comment for this project does not exit.  Please click on View History link for previous comments.";
                        else
                            currentValue += originalCommentsValue;

                    currentValue += "</span></a></span>";

                    dataRow[ currentFieldName ] = currentValue;
                }
            }
        }

        public void GetAndPopulateAllFieldValues( SPWeb storeWeb, List<DepartmentViewReferences> uniqueDataAccess, string sourceDepartment )
        {
            DataTable dtMain = new DataTable( );

            dtMain.Columns.Add( "ListName", typeof( string ) );
            dtMain.Columns.Add( "FieldName", typeof( string ) );
            dtMain.Columns.Add( "FieldValue", typeof( string ) );
            dtMain.Columns.Add( "FieldType", typeof( string ) );
            dtMain.Columns.Add( "InternalFieldName", typeof( string ) );
            dtMain.Columns.Add( "ReadOnly", typeof( string ) );
            DataRow drFixed;
            DataRow dr;
            DataRow[ ] drArray;
            SPQuery nonUserDepartments = new SPQuery( );

            SPList departmentListValues = storeWeb.Lists[ "Department Ordering Lookup" ];
            nonUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + sourceDepartment + "</Value></Eq><And><Neq><FieldRef Name='Source_x0020_Department' /><Value Type='Text'>" + sourceDepartment + "</Value></Neq><Neq><FieldRef Name='ReadOnly' /><Value Type='Boolean'>1</Value></Neq></And></And></Where><OrderBy><FieldRef Name='Order_x0020_Number' Ascending='True' /></OrderBy>";
            SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
            foreach ( SPListItem item in otherDepartments )
            {
                dr = dtMain.NewRow( );
                dr[ "ListName" ] = item[ "Source Department" ].ToString( );
                dr[ "FieldName" ] = item[ "Field Name" ].ToString( );
                dr[ "ReadOnly" ] = item[ "ReadOnly" ].ToString( );
                dr[ "InternalFieldName" ] = item[ "Internal Field Name" ].ToString( );
                drFixed = null;
                foreach ( DepartmentViewReferences deptCollections in uniqueDataAccess )
                {
                    if ( deptCollections.DepartmentName.Equals( item[ "Source Department" ].ToString( ) ) )
                    {
                        // currentProjectID
                        if ( deptCollections.DepartmentName.Equals( "Stores Detail" ) )
                        {
                            drArray = deptCollections.DepartmentDataTable.Select( "Reference_x0020__x0023_='" + currentProjectID + "'" );
                            drFixed = drArray[ 0 ];
                            break;
                        }
                        else
                        {
                            drArray = deptCollections.DepartmentDataTable.Select( "Store_x0020_Reference_x0020_ID='" + currentProjectID + "'" );
                            drFixed = drArray[ 0 ];
                            break;
                        }
                    }
                }

                dr[ "FieldValue" ] = Strip( DateStringCleanup( drFixed[ item[ "Internal Field Name" ].ToString( ) ].ToString( ) ) );
                dr[ "FieldType" ] = item[ "FieldType" ];
                dtMain.Rows.Add( dr );
            }
            viewAllFieldsForSourceDepartment.DataSource = dtMain;
            viewAllFieldsForSourceDepartment.DataBind( );
        }

        public string GetIDFromProjectID( string projectID, string departmentName )
        {
            string siteURL = SPContext.Current.Web.Url;
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPQuery singleDepartmentDepartments = new SPQuery( );
                    SPList localDepartmentData = storeWeb.Lists[ departmentName ];

                    if ( departmentName.Equals( "Stores Detail" ) )
                    {
                        singleDepartmentDepartments.Query = "<Where><Eq><FieldRef Name='Reference_x0020__x0023_' /><Value Type='Number'>" + projectID.Trim( ) + "</Value></Eq></Where>";
                        singleDepartmentDepartments.ViewFields = "<FieldRef Name='ID'/>";
                    }
                    else
                    {
                        singleDepartmentDepartments.Query = "<Where><Eq><FieldRef Name='Store_x0020_Reference_x0020_ID' /><Value Type='Number'>" + projectID.Trim( ) + "</Value></Eq></Where>";
                        singleDepartmentDepartments.ViewFields = "<FieldRef Name='ID'/>";
                    }

                    SPListItemCollection departmentsID = localDepartmentData.GetItems( singleDepartmentDepartments );
                    return departmentsID[ 0 ][ "ID" ].ToString( );
                }
            }
        }

        public string Strip( string text )
        {
            return Regex.Replace( text, @"<(.|\n)*?>", string.Empty );
        }

        //Dual Save Postback refernce
        protected string GetTestPostBack( )
        {
            return Page.ClientScript.GetPostBackEventReference( testClick, string.Empty );
        }

        protected override void OnInit( EventArgs e )
        {
            string siteURL = SPContext.Current.Web.Url;
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    if ( Request[ "ID" ] != null )
                        if ( Request[ "ID" ].ToString( ) != string.Empty )
                            currentItemID = Request[ "ID" ].ToString( );

                    if ( Request[ "ProjectID" ] != null )
                        if ( Request[ "ProjectID" ].ToString( ) != string.Empty )
                            currentProjectID = Request[ "ProjectID" ].ToString( );

                    if ( Request[ "Source" ] != null )
                    {
                        if ( Request[ "Source" ].ToString( ) != string.Empty )
                        {
                            string[ ] arr = Request[ "Source" ].ToString( ).Split( "?".ToCharArray( ) );
                            string[ ] q = arr[ 1 ].Split( "%26".ToCharArray( ) );
                            for ( int i = 0; i < q.Length; i++ )
                            {
                                if ( q[ i ].IndexOf( "currentDepartment" ) >= 0 )
                                {
                                    if ( q[ i ].IndexOf( "&" ) >= 0 )
                                    {
                                        string[ ] w = q[ i ].Split( "&".ToCharArray( ) );
                                        string[ ] z = w[ 0 ].Split( "=".ToCharArray( ) );
                                        currentDepartmentName = z[ 1 ];
                                    }
                                    else
                                    {
                                        string[ ] z = q[ i ].Split( "=".ToCharArray( ) );
                                        currentDepartmentName = z[ 1 ];
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if ( Request[ "currentDepartment" ] != null )
                            if ( Request[ "currentDepartment" ].ToString( ) != string.Empty )
                                currentDepartmentName = Request[ "currentDepartment" ].ToString( );
                    }

                    if ( currentDepartmentName != string.Empty && currentItemID != string.Empty && currentProjectID != string.Empty )
                        DisplayFields( storeWeb );
                }
            }
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            //method onInit acting as Page_Load due to single button click saving on department edit pages.
        }

        protected void saveChanges_Click( object sender, EventArgs e )
        {
            List<WorkflowItemIDs> itemLists = new List<WorkflowItemIDs>( );
            WorkflowItemIDs currentWFitem = new WorkflowItemIDs( );
            SPListItem currentItemInternalID;

            Control newValue = new Control( );
            Label holderFieldName = new Label( );
            Label holderProjectID = new Label( );
            Label holderListName = new Label( );
            Label holderFieldType = new Label( );
            Label holderInternalFieldName = new Label( );

            string stringholderFieldName = "";
            string stringholderProjectID = "";
            string stringholderListName = "";
            string stringholderFieldType = "";
            string stringholderInternalFieldName = "";

            string stringNewValue = "";

            string siteURL = SPContext.Current.Web.Url;
            using ( SPSite site = new SPSite( siteURL ) )
            {
                SPWeb web = SPContext.Current.Web;
                SPUser currentUser = web.CurrentUser;
                site.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = true;

                SPUserToken userToken = null;
                SPSecurity.RunWithElevatedPrivileges( delegate( )
                {
                    userToken = web.AllUsers[ "svuent\\eqpmoss" ].UserToken;

                } );

                using ( SPSite storeSite = new SPSite( site.ID, userToken ) )
                {
                    storeSite.AllowUnsafeUpdates = true;
                    using ( SPWeb storeWeb = storeSite.OpenWeb( web.ID ) )
                    {
                        storeWeb.AllowUnsafeUpdates = true;

                        foreach ( RepeaterItem dataItem in viewAllFieldsForSourceDepartment.Items )
                        {
                            holderProjectID = ( Label ) dataItem.FindControl( "ProjectID" );
                            if ( holderProjectID != null )
                                stringholderProjectID = holderProjectID.Text;

                            holderFieldName = ( Label ) dataItem.FindControl( "FieldName" );
                            if ( holderFieldName != null )
                                stringholderFieldName = holderFieldName.Text;

                            holderFieldType = ( Label ) dataItem.FindControl( "FieldType" );
                            if ( holderFieldType != null )
                                stringholderFieldType = holderFieldType.Text;

                            holderInternalFieldName = ( Label ) dataItem.FindControl( "InternalFieldName" );
                            if ( holderInternalFieldName != null )
                                stringholderInternalFieldName = holderInternalFieldName.Text;

                            holderListName = ( Label ) dataItem.FindControl( "ListName" );
                            if ( holderListName != null )
                                stringholderListName = holderListName.Text;
                            else
                                stringholderListName = "Error List Name";
                            newValue = dataItem.FindControl( "displayControl" );

                            switch ( stringholderFieldType )
                            {
                                case "Text":
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                                case "DateTime":
                                    if ( newValue != null )
                                    {
                                        if ( ( ( DateTimeControl ) newValue ).IsDateEmpty )
                                            stringNewValue = "";
                                        else
                                            stringNewValue = ( ( DateTimeControl ) newValue ).SelectedDate.ToShortDateString( );
                                    }
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Choice":
                                    if ( newValue != null )
                                        stringNewValue = ( ( DropDownList ) newValue ).SelectedValue.ToString( );
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Note":
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Lookup":
                                    if ( newValue != null )
                                        stringNewValue = ( ( DropDownList ) newValue ).SelectedValue.ToString( );
                                    else
                                        stringNewValue = "";
                                    break;
                                default:
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                            }

                            currentItemInternalID = SaveField( storeWeb, stringholderFieldName, stringholderProjectID, stringholderListName, stringNewValue, stringholderInternalFieldName, stringholderFieldType, currentUser );
                        }
                    }
                }
                holderResave.Text = "1";
            }
        }

        //Dual Save
        protected void testClick_Click( object sender, EventArgs e )
        {
            List<WorkflowItemIDs> itemLists = new List<WorkflowItemIDs>( );
            WorkflowItemIDs currentWFitem = new WorkflowItemIDs( );
            SPListItem currentItemInternalID;

            Control newValue = new Control( );
            Label holderFieldName = new Label( );
            Label holderProjectID = new Label( );
            Label holderListName = new Label( );
            Label holderFieldType = new Label( );
            Label holderInternalFieldName = new Label( );

            string stringholderFieldName = "";
            string stringholderProjectID = "";
            string stringholderListName = "";
            string stringholderFieldType = "";
            string stringholderInternalFieldName = "";

            string stringNewValue = "";

            string siteURL = SPContext.Current.Web.Url;
            using ( SPSite site = new SPSite( siteURL ) )
            {
                SPWeb web = SPContext.Current.Web;
                SPUser currentUser = web.CurrentUser;
                site.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = true;

                SPUserToken userToken = null;
                SPSecurity.RunWithElevatedPrivileges( delegate( )
                {
                    userToken = web.AllUsers[ "svuent\\eqpmoss" ].UserToken;
                } );
                using ( SPSite storeSite = new SPSite( site.ID, userToken ) )
                {
                    storeSite.AllowUnsafeUpdates = true;
                    using ( SPWeb storeWeb = storeSite.OpenWeb( web.ID ) )
                    {
                        storeWeb.AllowUnsafeUpdates = true;

                        foreach ( RepeaterItem dataItem in viewAllFieldsForSourceDepartment.Items )
                        {

                            holderProjectID = ( Label ) dataItem.FindControl( "ProjectID" );
                            if ( holderProjectID != null )
                                stringholderProjectID = holderProjectID.Text;

                            holderFieldName = ( Label ) dataItem.FindControl( "FieldName" );
                            if ( holderFieldName != null )
                                stringholderFieldName = holderFieldName.Text;

                            holderFieldType = ( Label ) dataItem.FindControl( "FieldType" );
                            if ( holderFieldType != null )
                                stringholderFieldType = holderFieldType.Text;

                            holderInternalFieldName = ( Label ) dataItem.FindControl( "InternalFieldName" );
                            if ( holderInternalFieldName != null )
                                stringholderInternalFieldName = holderInternalFieldName.Text;

                            holderListName = ( Label ) dataItem.FindControl( "ListName" );
                            if ( holderListName != null )
                                stringholderListName = holderListName.Text;
                            else
                                stringholderListName = "Error List Name";
                            newValue = dataItem.FindControl( "displayControl" );

                            switch ( stringholderFieldType )
                            {
                                case "Text":
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                                case "DateTime":
                                    if ( newValue != null )
                                    {
                                        if ( ( ( DateTimeControl ) newValue ).IsDateEmpty )
                                            stringNewValue = "";
                                        else
                                            stringNewValue = ( ( DateTimeControl ) newValue ).SelectedDate.ToShortDateString( );
                                    }
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Choice":
                                    if ( newValue != null )
                                        stringNewValue = ( ( DropDownList ) newValue ).SelectedValue.ToString( );
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Note":
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                                case "Lookup":
                                    if ( newValue != null )
                                        stringNewValue = ( ( DropDownList ) newValue ).SelectedValue.ToString( );
                                    else
                                        stringNewValue = "";
                                    break;
                                default:
                                    if ( newValue != null )
                                        stringNewValue = ( ( TextBox ) newValue ).Text;
                                    else
                                        stringNewValue = "";
                                    break;
                            }

                            currentItemInternalID = SaveField( storeWeb, stringholderFieldName, stringholderProjectID, stringholderListName, stringNewValue, stringholderInternalFieldName, stringholderFieldType, currentUser );
                        }
                    }
                }
                holderResave.Text = "1";
            }
        }

        protected void viewAllFieldsForSourceDepartment_OnItemDataBound( object sender, RepeaterItemEventArgs e )
        {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            {
                string currentType = ( ( DataRowView ) e.Item.DataItem )[ "FieldType" ].ToString( );
                string currentReadonly = ( ( DataRowView ) e.Item.DataItem )[ "ReadOnly" ].ToString( );
                string currentFieldValue = ( ( DataRowView ) e.Item.DataItem )[ "FieldValue" ].ToString( );
                string currentListName = ( ( DataRowView ) e.Item.DataItem )[ "ListName" ].ToString( );
                string currentFieldName = ( ( DataRowView ) e.Item.DataItem )[ "FieldName" ].ToString( );
                string currentInternalFieldName = ( ( DataRowView ) e.Item.DataItem )[ "InternalFieldName" ].ToString( );
                Control commonControl = new Control( );

                if ( !bool.Parse( currentReadonly ) )
                {
                    commonControl = new Label( );
                    ( ( Label ) commonControl ).ID = "ProjectID";
                    ( ( Label ) commonControl ).Text = currentProjectID;
                    ( ( Label ) commonControl ).Visible = false;
                    e.Item.Controls.Add( commonControl );

                    commonControl = new Label( );
                    ( ( Label ) commonControl ).ID = "FieldName";
                    ( ( Label ) commonControl ).Text = currentFieldName;
                    ( ( Label ) commonControl ).Visible = false;
                    e.Item.Controls.Add( commonControl );

                    commonControl = new Label( );
                    ( ( Label ) commonControl ).ID = "FieldType";
                    ( ( Label ) commonControl ).Text = currentType;
                    ( ( Label ) commonControl ).Visible = false;
                    e.Item.Controls.Add( commonControl );

                    commonControl = new Label( );
                    ( ( Label ) commonControl ).ID = "InternalFieldName";
                    ( ( Label ) commonControl ).Text = currentInternalFieldName;
                    ( ( Label ) commonControl ).Visible = false;
                    e.Item.Controls.Add( commonControl );

                    commonControl = new Label( );
                    ( ( Label ) commonControl ).ID = "ListName";
                    ( ( Label ) commonControl ).Text = currentListName;
                    ( ( Label ) commonControl ).Visible = false;
                    e.Item.Controls.Add( commonControl );

                    switch ( currentType )
                    {
                        case "Text":
                            commonControl = new TextBox( );
                            ( ( TextBox ) commonControl ).Text = currentFieldValue;
                            break;
                        case "DateTime":
                            commonControl = new DateTimeControl( );
                            ( ( DateTimeControl ) commonControl ).DateOnly = true;
                            if ( currentFieldValue != string.Empty )
                            {

                                ( ( DateTimeControl ) commonControl ).SelectedDate = DateTime.Parse( currentFieldValue );
                            }
                            break;
                        case "Choice":
                            commonControl = new DropDownList( );
                            commonControl = ( DropDownList ) GetAllLookupValues( currentListName, currentFieldName, currentType );

                            if ( currentFieldValue != string.Empty )
                            {
                                ( ( DropDownList ) commonControl ).ClearSelection( );

                                ( ( DropDownList ) commonControl ).Items.FindByText( currentFieldValue ).Selected = true;
                            }
                            break;
                        case "Note":
                            commonControl = new TextBox( );
                            ( ( TextBox ) commonControl ).TextMode = TextBoxMode.MultiLine;
                            ( ( TextBox ) commonControl ).Text = currentFieldValue;
                            break;
                        case "Lookup":
                            commonControl = new DropDownList( );
                            commonControl = ( DropDownList ) GetAllLookupValues( currentListName, currentFieldName, currentType );

                            if ( currentFieldValue != string.Empty )
                            {
                                ( ( DropDownList ) commonControl ).ClearSelection( );

                                ( ( DropDownList ) commonControl ).Items.FindByText( currentFieldValue ).Selected = true;
                            }
                            break;
                        default:
                            commonControl = new TextBox( );
                            ( ( TextBox ) commonControl ).Text = currentFieldValue;
                            break;
                    }
                }

                commonControl.ID = "displayControl";
                e.Item.Controls.Add( commonControl );
            }
        }

        private string DateStringCleanup( string input )
        {
            DateTime date;
            bool ok = DateTime.TryParse( input, out date );
            if ( ok )
                return date.ToShortDateString( );
            else
                return string.Empty;
        }

        private void DisplayFields( SPWeb storeWeb )
        {
            List<DepartmentViewReferences> uniqueDataAccess = new List<DepartmentViewReferences>( );
            DepartmentInformationBL departments = new DepartmentInformationBL( );

            uniqueDataAccess = departments.SelectUniqueColumnDepartmentsValues( storeWeb, currentDepartmentName );

            string sourceDept = currentDepartmentName;
            foreach ( DepartmentViewReferences deptDataPull in uniqueDataAccess )
                deptDataPull.PopulateCurrentDataCollection( storeWeb, currentItemID, currentProjectID );

            GetAndPopulateAllFieldValues( storeWeb, uniqueDataAccess, sourceDept );
        }

        private DropDownList GetAllLookupValues( string currentListName, string currentFieldName, string currentFieldType )
        {
            string siteURL = SPContext.Current.Web.Url;
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DropDownList currentDropDown = new DropDownList( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList currentList = storeWeb.Lists[ currentListName ];
                    if ( currentFieldType.Equals( "Choice" ) )
                    {
                        SPFieldChoice field = ( SPFieldChoice ) currentList.Fields[ currentFieldName ];

                        StringCollection allChoices = field.Choices;

                        for ( int i = 0; i < allChoices.Count; i++ )
                            currentDropDown.Items.Add( new ListItem( allChoices[ i ], allChoices[ i ] ) );
                    }
                    if ( currentFieldType.Equals( "Lookup" ) )
                    {
                        SPFieldLookup field = ( SPFieldLookup ) currentList.Fields[ currentFieldName ];

                        SPList lookupList;
                        lookupList = storeWeb.Lists[ new Guid( field.LookupList ) ];
                        foreach ( SPListItem item in lookupList.Items )
                            currentDropDown.Items.Add( new ListItem( item[ field.LookupField ].ToString( ), item.ID.ToString( ) + ";#" + item[ field.LookupField ].ToString( ) ) );

                    }
                }

                return currentDropDown;
            }
        }

        private SPListItem SaveField( SPWeb storeWeb, string fieldName, string projectID, string listName, string newValue, string internalFieldName, string internalFieldType, SPUser currentUser )
        {
            SPListItem resultID = null;
            storeWeb.AllowUnsafeUpdates = true;

            SPList localList = storeWeb.Lists[ listName ];
            SPQuery oneItemOnly = new SPQuery( );

            if ( listName.Equals( "Stores Detail" ) )
                oneItemOnly.Query = "<Where><Eq><FieldRef Name='Reference_x0020__x0023_' /><Value Type='Number'>" + projectID + "</Value></Eq></Where>";
            else
                oneItemOnly.Query = "<Where><Eq><FieldRef Name='Store_x0020_Reference_x0020_ID' /><Value Type='Number'>" + projectID + "</Value></Eq></Where>";

            oneItemOnly.ViewFields = "<FieldRef Name='" + internalFieldName + "'/>";
            SPListItemCollection allItems = localList.GetItems( oneItemOnly );

            if ( allItems.Count == 0 )
                return null;

            SPListItem itemToUpdate = allItems[ 0 ];
            resultID = itemToUpdate;

            if ( itemToUpdate[ fieldName ] == null )
            {
                if ( newValue.Length != 0 )
                {
                    itemToUpdate[ fieldName ] = newValue;
                    itemToUpdate[ "Last Modified By" ] = currentUser;
                    itemToUpdate.Update( );
                }
            }
            else
            {
                if ( Strip( DateStringCleanup( itemToUpdate[ fieldName ].ToString( ) ) ) != newValue )
                {
                    if ( newValue == string.Empty && internalFieldType == "DateTime" )
                        newValue = null;

                    itemToUpdate[ fieldName ] = newValue;
                    itemToUpdate[ "Last Modified By" ] = currentUser;
                    itemToUpdate.Update( );
                }
                else
                {
                    if ( internalFieldType == "DateTime" )
                    {
                        if ( itemToUpdate[ fieldName ].ToString( ) != newValue )
                        {
                            itemToUpdate[ fieldName ] = newValue;
                            itemToUpdate[ "Last Modified By" ] = currentUser;
                            itemToUpdate.Update( );
                        }
                    }
                    else
                    {
                        if ( itemToUpdate[ fieldName ].ToString( ) != newValue )
                        {
                            itemToUpdate[ fieldName ] = newValue;
                            itemToUpdate[ "Last Modified By" ] = currentUser;

                            itemToUpdate.Update( );
                        }
                    }
                }
            }
            localList.Update( );

            return resultID;
        }

        #endregion Methods
    }
}